#!/usr/bin/python
'Master server that receives data from nodes and renders it to HTML.'
import server
import master.handler
import master.nodes
import neighbours
import time

DO_GET_NEW_NODES = False

if __name__=='__main__':
    try:
        # cache path override hack
        neighbours.CACHE_FILE = 'neighbours.cache'
        neighbours.load('neighbours.cache')

        if DO_GET_NEW_NODES:
            # check for node updates 
            print 'Getting node list...'
            nodes = master.nodes.get_hostnames()
            print 'Retrieved',len(nodes),'nodes.'
            # are these nodes 'new'?
            for n in nodes:
                if n not in neighbours.nodes:
                    # node list is different!
                    # save it, make new timestamp
                    print 'Node list is new. Updating local copy.'
                    neighbours.set(int(time.time()), nodes)
                    break
    except:
        print 'Failed to get node list. Going with cache.'
    try:
        server.loop(master.handler.handler) #yuck!
    except KeyboardInterrupt:
        print 'Exiting!'
